﻿using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SqlClient;
using Model.Filters;
using Model.Query_Model;
using Datasource;

namespace Presenters.Managers
{
    public static class CurrencyManager
    {
        private static IList<CurrencyOutput> currencyList = new List<CurrencyOutput>();

        private static IList<CurrencyOutput> BuildCollections(DbDataReader reader)
        {
            currencyList.Clear();
            while (reader.Read())
            {
                currencyList.Add(new CurrencyOutput
                {
                    idCurrency=reader.GetInt32(0),
                    shortcut=reader.GetString(1),
                    fullName=reader.GetString(2)
                }
                );
            }
            return currencyList;
        }

        public static IList<CurrencyOutput> CurrencySearch(CurrencyCriteria criteria)
        {
            DbConnection connection = DbUtil.GetActivityConnection();
            DbCommand command = new SqlCommand();
            DbUtil.SetConnection(connection, command);
            DbUtil.SetQuery(command, new DbQuery { sql = "exec dbo.[CurrencySearch] @idValuation" });
            DbUtil.SetParametr(command, "@idValuation", criteria.idValuation);
            return BuildCollections(DbUtil.ExecuteReader(command));
        }
    }
}
